//Given an array of integers, return indices of the two numbers such that they add up to a specific target.
//You may assume that each input would have exactly one solution, and you may not use the same element twice.
var twoSum = function(nums, target) {
 				for(var i = 0; i < nums.length; ++i) {
					var pos1 = i;
					var data1 = nums[i];
					var data2 = target - nums[i];
					if(data2<=0) return null;
					else{		
						for(var j = i+1; j < nums.length; ++j) {
							if(data2 == nums[j]){
								return [pos1,j];								
							}

						}						
					}
				}
				return null;
}   
var nums = [2,7,11,15];
var tagrget = 9;
var result = twoSum(nums,tagrget);
alert(result);


//Given a 32-bit signed integer, reverse digits of an integer.
var reverse = function(x) {
    var temp  = x;//创建一个临时变量保存X的值
    var y = 0;//创建一个容器存储x值的最低位    
    while(x)
        {
            y = y*10+x%10;
            if((y-x%10)/10 != temp){
                return 0;
            }
             x/=10;
        }
    return y;
};
var x = 123;
var result = reverse(x);
alert(result);

//Determine whether an integer is a palindrome. 
//An integer is a palindrome when it reads the same backward as forward.
var isPalindrome = function(x) {
    if(x<0)
        return false;
    //若x为负数，则不是回文数
    
    var div = 1;
    while(x/10>=10)
        {
            div *=10;
        }
    //判断x的位数，这利于分离出首位数字以及末位数字
    
    while(x!=0)
        {
            var left = x/div;
            var right = x%10;
            if(left!=right)
                return false;
            //检查首位以及末位数字是否相同
            
            x = (x%div)/10;
            div /=100;
            //去掉首位以及末位数字
        }
    
    return true;
};
var x = 121;
var result = isPalindrome(x);
alert(result);

//Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
//
//An input string is valid if:
//
//Open brackets must be closed by the same type of brackets.
//Open brackets must be closed in the correct order.
//Note that an empty string is also considered valid.
var isValid = function(s) {
    var stack = [];
    if(s.length)
        {
             for(var i = 0;i<s.lenrth;i++)
            {
                var c = s.charAt(i);//通过此库函数获取当前位置的字符

                if(c=="("||c=="["||c =="{")
                    stack.push(c);//右侧括号，压栈

    var oldC = stack.pop();
    if(oldC == '(' && c != ')')  
                return false;  
    else if(oldC == '[' && c != ']')  
                return false;  
    else if(oldC == '{' && c != '}')
                return false;  
            }
        }
    return stack.length === 0;
    };
var s = "()"
var result = isValid(s);
alert(result);

//You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.
//Given n, find the total number of full staircase rows that can be formed.
//n is a non-negative integer and fits within the range of a 32-bit signed integer.
var arrangeCoins = function(n) {
    var cnt = 0;
    var i = 1;
    while(n>=i)
        {
            cnt++;
            n = n-i;
            i++;
        }
    return cnt;
};

var n = 5;
var result = arrangeCoins(n);
alert(result);

//Assume you are an awesome parent and want to give your children some cookies. 
//But, you should give each child at most one cookie. Each child i has a greed factor gi, 
//which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. 
//If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. 
//Your goal is to maximize the number of your content children and output the maximum number.
//Note:
//You may assume the greed factor is always positive. 
//You cannot assign more than one cookie to one child.
var findContentChildren = function(g, s) {
    //首先对两个数组进行排序，将小的点心分给胃口小的小朋友
    //依次匹配，用计数器记录，知道不匹配
    g.sort();
    s.sort();
   var i = 0;
   var j = 0;
    
    for ( i = 0; j < g.length && i<s.length; ++i) {
            if (s[i] >= g[j]) ++j;
        }
        return j;
    
};

var g = [1,2,3];
var s = [1,2];
var result = findContentChildren(g,s);